package com.shopng.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shopng.mapper.UserMapper;
import com.shopng.model.User;

/**
 * 
 * @author sgao20
 *
 */
@Service
public class UserService
{
  
  @Autowired
  private UserMapper userMapper;
  
  public List<User> findAll()
  {
    return userMapper.selectAll();
  }
  
  public int create(User user)
  {
    user.setRegTime(new Date());
    user.setAvailableMoney(BigDecimal.ZERO);
    user.setAvailableRcBalance(BigDecimal.ZERO);
    user.setExppoints(0);
    user.setLoginNum(1);
    user.setLoginTime(new Date());
    return userMapper.insertSelective(user);
  }
  
  public User findById(Integer id)
  {
    return userMapper.selectByPrimaryKey(id);
  }
  
  public User login(String email, String password)
  {
    User record = new User();
    record.setName(email);
    record.setPassword(password);
    return userMapper.selectOne(record);
  }
  
  public User update(User user)
  {
    userMapper.updateByPrimaryKey(user);
    return null;
  }
  
  public void deleteUser(Integer id)
  {
    userMapper.deleteByPrimaryKey(id);
  }
  
  public User findUserByName(String name)
  {
    return userMapper.findUserByName(name);
  }
  
}
